﻿using GodSystemBLL;
using GodSystemCommon;
using GodSystemModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GodSystem.FormChildren
{
    public partial class FormRoleModify : Form
    {
        private static FormRoleModify formRoleModify = null;
        public FormRoleModify()
        {
            InitializeComponent();
            InitData();
        }

        private void InitData()
        {
            // 展示角色信息
            string roleName = FormRolePower.rolesModel.RoleName;
            int id = FormRolePower.rolesModel.Id;
            textBox_RoleName.Text = roleName;
            if (!Utility.CheckUserPower("RoleUpdate"))
            {
                textBox_RoleName.Enabled = false;
                button_RoleModify.Visible = false;
                button_Cancel.Visible = false;
            }
            groupBox1.Text = "角色 【"+ roleName + "】修改";

            bool hasUpdateRolePower = Utility.CheckUserPower("UpdateRolePower");
            if (!hasUpdateRolePower)
            {
                button_PowerUpdate.Visible = false;
            }

            PowersBLL powersBLL = new PowersBLL();

            // 获取角色拥有的权限
            Dictionary<int, int> map = new Dictionary<int, int>();
            List<RolepowerModel> roleList = powersBLL.GetRolePower(id);
            foreach (RolepowerModel rolepowermodel in roleList)
            {
                map.Add(rolepowermodel.PowerId, rolepowermodel.RoleId);
            }
            // 展示权限列表
            List<PowersModel> powerList = powersBLL.GetPowersList();
            int j = 0;
            int startLocationX = 20;
            int startLocationY = 20;
            int sizeX = 100;
            int sizeY = 20;
            int sep = 0;
            int flag = 1;
            // 记录遍历过的组ID
            List<int> hasCheckGroupId = new List<int>();
            // 保存权限组
            Dictionary<int, string> powerGroup = new Dictionary<int, string>();

            List<PowerGroupModel> pgList = powersBLL.GetPowerGroup();
            for(int i = 0; i < pgList.Count; i++)
            {
                powerGroup.Add(pgList[i].Id, pgList[i].GroupName);
            }

            for (int i = 0; i < powerList.Count; i++)
            {
               //排列指定的数量就换行,并且Y坐标增加
                if (j > 0 && flag % 6 == 0)
                {
                    sep++;
                    j = 0;
                }
                flag++;
                PowersModel power = powerList[i];

                if (!hasCheckGroupId.Contains(power.PowerGroupId))
                {
                    hasCheckGroupId.Add(power.PowerGroupId);
                    Label labelTag = new Label();
                    labelTag.Anchor = AnchorStyles.Left | AnchorStyles.Top;
                    if (powerGroup.ContainsKey(power.PowerGroupId))
                    {
                        labelTag.Text = powerGroup[power.PowerGroupId];
                    }
                    else
                    {
                        powerGroup.Add(power.PowerGroupId, "未分组");
                        labelTag.Text = "未分组";
                    }
                    
                    labelTag.ForeColor = Color.Brown;
                    labelTag.Font = new Font(FontFamily.GenericSansSerif, 16);
                    // 没有换行时，另起一行
                    if(j != 0)
                    {
                        sep++;
                    }
                    int labelLocationY = startLocationY + (startLocationY + sizeY) * sep;
                    labelTag.Location = new Point(20, labelLocationY);
                    labelTag.Size = new Size(sizeX, sizeY + 10);
                    panel_PowerBox.Controls.Add(labelTag);
                    // 再增一行
                    sep++;
                    j = 0;
                    flag = 1;
                }
               
                int newstartLocationX = startLocationX + (startLocationX + sizeX) * j;
                int newstartLocationY = startLocationY + (startLocationY + sizeY) * sep;

                CheckBox checkBox = new CheckBox();
                checkBox.Anchor = AnchorStyles.Left | AnchorStyles.Top;
                checkBox.Location = new Point(newstartLocationX, newstartLocationY);
                checkBox.Size = new Size(sizeX, sizeY);
                checkBox.Text = power.Remark;
                checkBox.Tag = power;
                if (!hasUpdateRolePower)
                {
                    checkBox.Enabled = false;
                }
                if (map.ContainsKey(power.Id))
                {
                    checkBox.Checked = true;
                }
                
                checkBox.Name = "power-" + power.Id;
                j++;
                panel_PowerBox.Controls.Add(checkBox);
            }
            
        }

        public static FormRoleModify CreateInstance()
        {
            if (formRoleModify == null || formRoleModify.IsDisposed)
            {
                formRoleModify = new FormRoleModify();
            }
            return formRoleModify;
        }

        private void button_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
        }

        private void button_RoleModify_Click(object sender, EventArgs e)
        {
            string newRoleName = textBox_RoleName.Text.Trim();
            if (string.IsNullOrEmpty(newRoleName))
            {
                MessageBox.Show("角色名称不能为空","提示", MessageBoxButtons.OK,MessageBoxIcon.Warning);
                return;
            }
            if (newRoleName.Equals(FormRolePower.rolesModel.RoleName))
            {
                MessageBox.Show("角色名称一致，不用修改", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            RolesBLL rolesBLL = new RolesBLL();
            RolesModel rolesModel = new RolesModel();
            rolesModel.RoleName = newRoleName;
            int num = Convert.ToInt32(rolesBLL.GetRole(rolesModel));
            if(num > 0)
            {
                MessageBox.Show("角色名称已经存在", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            rolesModel.RoleName = FormRolePower.rolesModel.RoleName;
            int updateNum = rolesBLL.UpdateRole(rolesModel, newRoleName);
            if (updateNum > 0)
            {
                MessageBox.Show("修改成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
        }
        /// <summary>
        /// 更新角色权限
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_PowerUpdate_Click(object sender, EventArgs e)
        {
            List<int>  powerId = new List<int>();
            // 遍历区别内的元素
            foreach(Control ck in panel_PowerBox.Controls)
            {
                // 是否为指定的控件
                if(ck is CheckBox)
                {
                    if (((CheckBox)ck).Checked)
                    {
                        PowersModel powers =  (PowersModel) ck.Tag;
                        powerId.Add(powers.Id);
                    }
                }
                
            }
            int roleId = FormRolePower.rolesModel.Id;
            PowersBLL powersBLL = new PowersBLL();
            int result = powersBLL.UpdateRolePower(powerId,roleId);
            if (result > 0)
            {
                MessageBox.Show("修改成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("修改失败，发生异常，请检测", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
